package com.pro01.backend.mapper;

import com.pro01.backend.pojo.ForumComment;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ForumCommentMapper {

    // 用户发表评论
    @Insert("""
        INSERT INTO forum_comments (post_id, user_id, content, parent_comment_id, status, created_at)
        VALUES (#{postId}, #{userId}, #{content}, #{parentCommentId}, #{status}, NOW())
    """)
    int insertComment(ForumComment comment);

    // 用户分页查询评论
//    @Select("""
//        SELECT * FROM forum_comments
//        WHERE post_id = #{postId}
//          AND status = 'ACTIVE'
//        ORDER BY created_at ASC
//        LIMIT #{offset}, #{pageSize}
//    """)
//    List<ForumComment> selectCommentsByPost(@Param("postId") Long postId,
//                                            @Param("offset") int offset,
//                                            @Param("pageSize") int pageSize);
    @Select("""
    SELECT c.*, u.username, u.title AS userTitle,
           u.nickname_color AS nicknameColor, u.avatar_frame AS avatarFrame
    FROM forum_comments c
    LEFT JOIN users u ON c.user_id = u.id
    WHERE c.post_id = #{postId}
      AND c.status = 'ACTIVE'
    ORDER BY c.created_at ASC
    LIMIT #{offset}, #{pageSize}
""")
    List<ForumComment> selectCommentsByPost(@Param("postId") Long postId,
                                            @Param("offset") int offset,
                                            @Param("pageSize") int pageSize);

    @Select("""
        SELECT COUNT(*)
        FROM forum_comments
        WHERE post_id = #{postId}
          AND status = 'ACTIVE'
    """)
    int countCommentsByPost(@Param("postId") Long postId);

    // 用户删除自己的评论
    @Update("""
    UPDATE forum_comments
    SET status = 'DELETED'
    WHERE id = #{commentId}
      AND user_id = #{userId}
      AND status = 'ACTIVE'
""")
    int deleteCommentByUser(@Param("commentId") Long commentId,
                            @Param("userId") Long userId);

    @Select("SELECT * FROM forum_comments WHERE id = #{commentId}")
    ForumComment getCommentById(@Param("commentId") Long commentId);

    // 用户隐藏自己的评论
    @Update("""
    UPDATE forum_comments
    SET status = 'HIDDEN'
    WHERE id = #{commentId}
      AND user_id = #{userId}
      AND status = 'ACTIVE'
""")
    int hideCommentByUser(@Param("commentId") Long commentId,
                          @Param("userId") Long userId);

    // 用户恢复自己隐藏的评论
    @Update("""
    UPDATE forum_comments
    SET status = 'ACTIVE'
    WHERE id = #{commentId}
      AND user_id = #{userId}
      AND status = 'HIDDEN'
""")
    int restoreCommentByUser(@Param("commentId") Long commentId,
                             @Param("userId") Long userId);

    // 用户查询自己发表的评论
    @Select("""
    SELECT * FROM forum_comments
    WHERE post_id = #{postId}
      AND user_id = #{userId}
      AND status = 'ACTIVE'
    ORDER BY created_at ASC
    LIMIT #{offset}, #{pageSize}
""")
    List<ForumComment> selectMyCommentsByPost(@Param("postId") Long postId,
                                              @Param("userId") Long userId,
                                              @Param("offset") int offset,
                                              @Param("pageSize") int pageSize);

    @Select("""
    SELECT COUNT(*)
    FROM forum_comments
    WHERE post_id = #{postId}
      AND user_id = #{userId}
      AND status = 'ACTIVE'
""")
    int countMyCommentsByPost(@Param("postId") Long postId,
                              @Param("userId") Long userId);

    /**
     * 下面是管理员的接口
     */
    // 删除评论(仅限管理员)
    @Update("""
    UPDATE forum_comments
    SET status = 'DELETED'
    WHERE id = #{commentId}
      AND status != 'DELETED'
""")
    int deleteCommentByAdmin(@Param("commentId") Long commentId);

    // 隐藏评论(仅限管理员)
    @Update("""
    UPDATE forum_comments
    SET status = 'HIDDEN'
    WHERE id = #{commentId}
      AND status = 'ACTIVE'
""")
    int hideCommentByAdmin(@Param("commentId") Long commentId);

    // 恢复评论(仅限管理员)
    @Update("""
    UPDATE forum_comments
    SET status = 'ACTIVE'
    WHERE id = #{commentId}
      AND status IN ('HIDDEN', 'DELETED')
""")
    int restoreCommentByAdmin(@Param("commentId") Long commentId);

}
